package com.springboot.frame.excel.util;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author madman
 */
public final class ValidationKit {

  /**
   * 验证器：Validation的实现框架
   */
  private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory()
      .getValidator();

  private ValidationKit() {
  }

  /**
   * 验证对象
   *
   * @param object 对象
   * @return 返回错误验证信息
   */
  public static String validate(Object object) {
    Set<ConstraintViolation<Object>> constraintViolations = VALIDATOR.validate(object);
    StringBuilder result = new StringBuilder();
    for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
      result.append(constraintViolation.getMessage()).append(";");
    }
    return result.toString();
  }

}
